package com.xiaoke.validator;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaoke.annotation.TagExist;
import com.xiaoke.domain.entity.Tag;
import com.xiaoke.service.TagService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import java.util.Set;
import java.util.stream.Collectors;

import static com.xiaoke.constants.SystemConstants.STATUS_NORMAL;


@Component
public class TagExistValidator implements ConstraintValidator<TagExist, Set> {

    @Resource
    TagService tagService;

    @Override
    public boolean isValid(Set set, ConstraintValidatorContext constraintValidatorContext) {
        if(set ==  null) {
            return true;
        }
        for (Object o : set) {
            LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Tag::getStatus,STATUS_NORMAL)
                    .eq(Tag::getId,o);
            int count = tagService.count(queryWrapper);
            if(count == 0) {
                return  false;
            }
        }
        return true;
    }
}
